package com.schoolgard.action.alarm;

import net.sf.json.JSONObject;

import org.apache.commons.lang3.StringUtils;

import com.schoolegard.services.MessageStrategyService;
import com.schoolgard.base.BaseAction;
import com.schoolgard.entitybean.GridDataModel;
import com.schoolgard.entitybean.MessageStrategy;
import com.supersystem.utils.JSONUtils;

public class MessageStrategyAction extends BaseAction {

	private MessageStrategyService strategyService;

	public void setStrategyService(MessageStrategyService strategyService) {
		this.strategyService = strategyService;
	}

	private MessageStrategy strategy;

	public void findAll() {
		String startStr = request.getParameter("start"); // grid传过来的当前数据条数
		String limitStr = request.getParameter("limit"); // 每页显示的数据数

		int start = Integer.parseInt(startStr);
		int limit = Integer.parseInt(limitStr);
		if (limit == 0) {
			limit = Integer.MAX_VALUE;
		}
		GridDataModel<MessageStrategy> page = strategyService.find4Page(strategy, start, limit);
		String jsonStr = JSONObject.fromObject(page).toString();
		writeToResponse(jsonStr);
	}

	public void save() {
		Long id = strategy.getId();
		MessageStrategy ms = null;
		if (id == null || id == 0) {
			ms = new MessageStrategy(); // 新增
		} else {
			ms = strategyService.findById(id);
		}
		ms.setAlarmLevel(strategy.getAlarmLevel());
		ms.setAlarmTimes(strategy.getAlarmTimes());
		ms.setAlarmTypeId(strategy.getAlarmTypeId());
		ms.setResendInterval(strategy.getResendInterval());
		ms.setResendTimes(strategy.getResendTimes());
		ms.setSendType(strategy.getSendType());
		strategyService.save(ms);
	}

	public void delete() {
		String id = request.getParameter("deleteId");
		if (StringUtils.isBlank(id)) {
			writeToResponse("0");
			return;
		}
		strategyService.delete(Long.valueOf(id));
		writeToResponse("1");
	}

	public void findByID() {
		String id = request.getParameter("strategyId");
		if (StringUtils.isEmpty(id)) {
			return;
		}
		MessageStrategy handle = strategyService.findById(Long.valueOf(id));
		writeToResponse(JSONUtils.toJSONString(handle));
	}

	public MessageStrategy getStrategy() {
		return strategy;
	}

	public void setStrategy(MessageStrategy strategy) {
		this.strategy = strategy;
	}

}
